package cn.pangza.user.service.impl;

import cn.pangza.common.mybatisplus.entity.PageRequest;
import cn.pangza.common.mybatisplus.entity.PageResult;
import cn.pangza.common.redis.service.RedisService;
import cn.pangza.common.utils.StringUtil;
import cn.pangza.user.constant.UserRedisKeyConstant;
import cn.pangza.user.convert.MenuConvert;
import cn.pangza.user.entity.dto.MenuDto;
import cn.pangza.user.entity.dto.MenuPageDto;
import cn.pangza.user.entity.po.Menu;
import cn.pangza.user.entity.po.SystemUserPo;
import cn.pangza.user.entity.vo.MenuChildrenVo;
import cn.pangza.user.entity.vo.MenuVo;
import cn.pangza.user.entity.vo.SystemUserVo;
import cn.pangza.user.mapper.MenuMapper;
import cn.pangza.user.service.MenuService;
import cn.pangza.user.utils.SecurityUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Autowired
    MenuMapper menuMapper;

    @Autowired
    RedisService redisService;

    @Override
    public List<Menu> getMenuListByUserId() {
        SystemUserPo currentSystemUserPo = SecurityUtil.getCurrentSystemUserPo();
        if(ObjectUtils.isEmpty(currentSystemUserPo)) {
            currentSystemUserPo = (SystemUserPo) SecurityUtil.getCurrentUser();
        }
        return menuMapper.getMenuListByUserId(currentSystemUserPo.getId());
    }

    @Override
    public List<Menu> getUserMenuWithChildren() {
        SystemUserPo currentSystemUserPo = SecurityUtil.getCurrentSystemUserPo();
        if(ObjectUtils.isEmpty(currentSystemUserPo)) {
            currentSystemUserPo = (SystemUserPo) SecurityUtil.getCurrentUser();
        }
        String menuListStr = redisService.get(UserRedisKeyConstant.USER_MENU + currentSystemUserPo.getId());
        if(StringUtil.isEmpty(menuListStr)) {
            List<Menu> userMenuWithChildren = menuMapper.getUserMenuWithChildren(currentSystemUserPo.getId(), -1);
            redisService.set(UserRedisKeyConstant.USER_MENU + currentSystemUserPo.getId(), JSONObject.toJSONString(userMenuWithChildren));
            return userMenuWithChildren;
        } else {
            return JSONObject.parseObject(menuListStr, List.class);
        }
    }

    @Override
    public PageResult<MenuVo> getMenuPage(PageRequest<MenuPageDto> pageRequest) {
        Page<Menu> menuPage = new Page<>(pageRequest.getPageNum(), pageRequest.getPageSize());
        QueryWrapper<Menu> menuQueryWrapper = new QueryWrapper<>();
        menuQueryWrapper.orderByDesc("order_num");
        if(ObjectUtils.isNotEmpty(pageRequest.getForm())) {
            if(ObjectUtils.isNotEmpty(pageRequest.getForm().getTitle())) {
                menuQueryWrapper.like("title", pageRequest.getForm().getTitle());
            }

            if(ObjectUtils.isNotEmpty(pageRequest.getForm().getName())) {
                menuQueryWrapper.like("name", pageRequest.getForm().getName());
            }

            if(ObjectUtils.isNotEmpty(pageRequest.getForm().getButton())) {
                menuQueryWrapper.eq("button", pageRequest.getForm().getButton());
            }

            if(ObjectUtils.isNotEmpty(pageRequest.getForm().getShow())) {
                menuQueryWrapper.eq("`show`", pageRequest.getForm().getShow());
            }

            if(ObjectUtils.isNotEmpty(pageRequest.getForm().getAuth())) {
                menuQueryWrapper.eq("auth", pageRequest.getForm().getAuth());
            }
        }

        Page<Menu> page = this.page(menuPage, menuQueryWrapper);
        Page<MenuVo> menuVoPage = new Page<>();
        BeanUtils.copyProperties(page, menuVoPage);
        return PageResult.generatePage(menuVoPage);
    }

    @Override
    public List<MenuChildrenVo> getMenuChildren(Integer button) {
        if(button == 1) {
            return menuMapper.getMenuChildrenAndButton(-1);
        } else {
            return menuMapper.getMenuChildren(-1);
        }
    }

    @Override
    public void createMenu(MenuDto menuDto) {
        Menu menu = MenuConvert.INSTANCE.menuDto2Menu(menuDto);
        if(ObjectUtils.isNotEmpty(menuDto.getAuth())) {
            menu.setAuth(menuDto.getAuth());
        }

        if(ObjectUtils.isNotEmpty(menuDto.getShow())) {
            menu.setShow(menuDto.getShow());
        }

        if(ObjectUtils.isNotEmpty(menuDto.getButton())) {
            menu.setButton(menuDto.getButton());
        }

        this.save(menu);
    }

    @Override
    public void updateMenu(MenuDto menuDto) {
        Menu menu = MenuConvert.INSTANCE.menuDto2Menu(menuDto);
        if(ObjectUtils.isNotEmpty(menuDto.getAuth())) {
            menu.setAuth(menuDto.getAuth());
        }

        if(ObjectUtils.isNotEmpty(menuDto.getShow())) {
            menu.setShow(menuDto.getShow());
        }

        if(ObjectUtils.isNotEmpty(menuDto.getButton())) {
            menu.setButton(menuDto.getButton());
        }
        this.updateById(menu);
    }
}
